/**
 * 十分彩的共用逻辑
 */

// 标题
initGame(game, function() {
    fill_zonghe_row();
    fill_lmp();
    fill_danhao();
    fill_zhengma();
    fill_lianma();
});

// 填充总和
function fill_zonghe_row() {
    var types = ["大", "单", "尾大", "小", "双", "尾小"];
    var table = $(".zonghe tbody");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 0; i < types.length; i++) {
        add_bet_item('和' + types[i], '总和' + types[i], tr);
        if (i == 2) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }
    table.append(tr);
}

// 填充8球的两面盘
function fill_lmp() {
    var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
    var types = ['大', '小', '单', '双', '尾大', '尾小', '合单', '合双', '龙', '虎'];
    var arr2 = ['大', '小', '单', '双', '尾大', '尾小', '合数单', '合数双', '龙', '虎'];
    var div = $(".eight");
    div.empty();

    for (var i in arr) {
        var table = $('<table class="container"></table>');
        table.append('<tr><th>第' + arr[i] + '球</th></tr>');
        for (var j = 0; j < types.length; j++) {
            if (j > 7 && i > 3) {
                continue;
            }
            var tr = $("<tr></tr>");
            add_bet_item(arr[i] + types[j], arr2[j], tr);
            table.append(tr);
        }

        var li = $("<li></li>");
        li.append(table);
        div.append(li);
    }
}

// 填充8球的单号
function fill_danhao() {
    var div = $(".danhao");
    div.empty();

    var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
    for (var z in arr) {
        var table = $('<table class="container"></table>');
        table.append('<tr><th colspan="4">第' + arr[z] + '球</th></tr>');
        for (var i = 1; i <= 5; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 15; j += 5) {
                var k = j < 10 ? "0" + j : j;
                if (game == 'cqkl10')
                    add_bet_item2(arr[z] + ":" + k, '<img src="../public/all/images/fruit_' + j + '.png"/>', tr);
                else
                    add_bet_item2(arr[z] + ":" + k, '<img src="../public/all/images/' + j + '.png"/>', tr);
            }
            table.append(tr);
        }
        div.append(table);
    }

    var types = ['大', '单', '尾大', '合单', '小', '双', '尾小', '合双', '东', '南', '西', '北', '中', '发', '白'];
    var tables = [];
    for (var i = 1; i <= 8; i++)
        tables.push($(".qiu" + i + " tbody"));
    for (var z in tables) {
        var table = tables[z];
        for (var i = 1; i <= 5; i++) {
            var tr = $("<tr></tr>");
            for (var j = i; j <= i + 15; j += 5) {
                var k = j < 10 ? "0" + j : j;
                if (game == 'cqkl10')
                    add_bet_item2(arr[z] + ":" + k, '<img src="../public/all/images/fruit_' + j + '.png"/>', tr);
                else
                    add_bet_item2(arr[z] + ":" + k, '<img src="../public/all/images/' + j + '.png"/>', tr);
            }
            table.append(tr);
        }

        var tr = $("<tr></tr>");
        for (var i in types) {
            var n = types[i];
            if (n == '合单') n = '合数单';
            if (n == '合双') n = '合数双';
            add_bet_item(arr[z] + types[i], n, tr);
            if (i % 4 == 3) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
        tr.append("<td> </td>");
        table.append(tr);

        if (z > 3) continue;
        var tr = $("<tr></tr>");
        add_bet_item(arr[z] + '龙', '龙', tr);
        add_bet_item(arr[z] + '虎', '虎', tr);
        tr.append("<td> </td>"); tr.append("<td> </td>");
        table.append(tr);
    }
}

// 填充正码
function fill_zhengma() {
    var table = $(".zhengma tbody");
    table.empty();
    for (var i = 1; i <= 5; i++) {
        var tr = $("<tr></tr>");
        for (var j = i; j <= i + 15; j += 5) {
            var k = j < 10 ? "0" + j : j;
            if (game == 'cqkl10')
                add_bet_item2("正:" + k, '<img src="../public/all/images/fruit_' + j + '.png"/>', tr);
            else
                add_bet_item2("正:" + k, '<img src="../public/all/images/' + j + '.png"/>', tr);
        }
        table.append(tr);
    }

}

// 填充连码
function fill_lianma() {
    var types = ['任2', '组2', '任3', '组3', '任4', '任5'];
    var table = $(".lianma thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<th><label class="am-radio"><input type="radio" name="radio" value="' + i + '" data-am-ucheck></label></th>');
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<td>' + gameRule.getTargetName(types[i]) + "</td>");
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        var key = types[i];
        var rate = gameRule.map[key] ? gameRule.map[key].rate : "--";
        tr.append('<td class="special-rate" key="' + key + '">' + rate + "</td>");
    }
    table.append(tr);
    $($(".lianma input")[0]).uCheck("check");

    // 填充20个号码
    table = $(".numbers thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 0; i < 4; i++) {
        tr.append("<th>号</th>");
        tr.append("<th>勾选</th>");
    }
    table.append(tr);

    table = $(".numbers tbody");
    table.empty();
    for (var i = 1; i <= 5; i++) {
        var tr = $("<tr></tr>");
        for (var j = i; j <= i + 15; j += 5) {
            if (game == 'cqkl10')
                tr.append("<td class='img'><img src='../public/all/images/fruit_" + j + ".png'></td>");
            else
                tr.append("<td class='img'><img src='../public/all/images/" + j + ".png'></td>");
            tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + j + '" data-am-ucheck></label></td>');
        }
        table.append(tr);
    }

    register_lianma_event();
}

// 连码的事件处理
function register_lianma_event() {
    var types = ['任2', '组2', '任3', '组3', '任4', '任5'];

    $(".lianma input").change(function(e) {
        clear_lianma();
    });
    $(".numbers input").change(function(e) {
        refresh_lianma_numbers();
    });
}

// 当前的投注类型
function get_lianma_target() {
    var types = ['任2', '组2', '任3', '组3', '任4', '任5'];
    var i = $(".lianma input:checked").val();
    return types[parseInt(i)];
}

// 取得当前选中的数字列表
function get_lianma_numbers() {
    var arr = $(".numbers input");
    var list = [];
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list.push($(arr[i]).attr("key"))
    }
    return list;
}

// 清理选中的数字
function clear_lianma() {
    $(".numbers input").uCheck('enable');
    $(".numbers input").uCheck('uncheck');
}

// 刷新连码的数字信息
function refresh_lianma_numbers() {
    var arr = $(".numbers input");
    var key = get_lianma_target();
    var list = get_lianma_numbers();
    var max = 7;
    if (key == '任4' || key == '任5') max = 5;

    arr.uCheck('enable');
    if (list.length >= max) {
        // 其他数字需要置灰
        for (var i = 0; i < arr.length; i++) {
            if (!arr[i].checked)
                $(arr[i]).uCheck('disable');
        }
    }

    for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
            $(arr[i]).parent().parent().attr("class", "selected");
        }
        else {
            $(arr[i]).parent().parent().attr("class", "");
        }
    }
}

// 确定投注的处理
function lianma_do_bet(v) {
    var key = get_lianma_target();
    var limit = parseInt(key[1]);
    var list = get_lianma_numbers();
    if (v < 1 || list.length < limit) {
        alert2("下注内容不正确，请重新下注！", true);
        return;
    }

    key = key + ":" + list.join(",");
    var data = {};
    data[key] = v * 100;
    show_bet_confirm_list(data);
}
function lianma_reset() {
    clear_lianma();
}

window.parse_target_name = function(k) {
    if (k.startsWith('任2')) {
        var arr = k.split(':');
        var numbers = arr[1].split(',');
        var i = numbers.length * (numbers.length - 1) / 2;
        return '任选二 ' + arr[1] + ' [' + i + '组]';
    }
    if (k.startsWith('任3')) {
        var arr = k.split(':');
        var numbers = arr[1].split(',');
        var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) / 6;
        return '任选三 ' + arr[1] + ' [' + i + '组]';
    }
    if (k.startsWith('任4')) {
        var arr = k.split(':');
        var numbers = arr[1].split(',');
        var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) * (numbers.length - 3) / 24;
        return '任选四 ' + arr[1] + ' [' + i + '组]';
    }
    if (k.startsWith('组2')) {
        var arr = k.split(':');
        var numbers = arr[1].split(',');
        var i = numbers.length * (numbers.length - 1) / 2;
        return '选二连组 ' + arr[1] + ' [' + i + '组]';
    }
    if (k.startsWith('组3')) {
        var arr = k.split(':');
        var numbers = arr[1].split(',');
        var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) / 6;
        return '选三前组 ' + arr[1] + ' [' + i + '组]';
    }
    return gameRule.getTargetName(k);
}
window.calc_bet_list = function(data) {
    var total = 0, count = 0;
    for (var k in data) {
        if (k.startsWith('组2') || k.startsWith('任2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) / 2;
            count += i;
            total += data[k] * i;
            continue;
        }
        if (k.startsWith('组3') || k.startsWith('任3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) / 6;
            count += i;
            total += data[k] * i;
            continue;
        }
        if (k.startsWith('任4')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = numbers.length * (numbers.length - 1) * (numbers.length - 2) * (numbers.length - 3) / 24;
            count += i;
            total += data[k] * i;
            continue;
        }
        count++;
        total += data[k];
    }
    return [count, total];
}
window.unpack_bet_list = function(data) {
    var r = {};
    for (var k in data) {
        if (k.startsWith('组2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var list = group2(numbers);
            for (var i in list) {
                r['组2:' + list[i].join(',')] = data[k];
            }
            r.group = '选二连组 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
        }
        else if (k.startsWith('组3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var list = group3(numbers);
            for (var i in list) {
                r['组3:' + list[i].join(',')] = data[k];
            }
            r.group = '选三前组 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
        }
        else if (k.startsWith('任2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var list = group2(numbers);
            for (var i in list) {
                r['任2:' + list[i].join(',')] = data[k];
            }
            r.group = '任选二 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
        }
        else if (k.startsWith('任3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var list = group3(numbers);
            for (var i in list) {
                r['任3:' + list[i].join(',')] = data[k];
            }
            r.group = '任选三 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
        }
        else if (k.startsWith('任4')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var list = group4(numbers);
            for (var i in list) {
                r['任4:' + list[i].join(',')] = data[k];
            }
            r.group = '任选四 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
        }
        else
            r[k] = data[k];
    }
    return r;
}
